#include "mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    this->mdiArea = new QMdiArea();
    this->mdiArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);
    this->mdiArea->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
    this->setCentralWidget(this->mdiArea);
    createActions();
    createMenus();
    createToolBars();
    createStatusBar();
    readSettings();
    this->setWindowTitle(tr("yjy Image tools"));
    this->setWindowIcon(QIcon(":/res/letter_L.png"));
}
void MainWindow::createActions()
{
    this->openfileAct = new QAction(QIcon(":/res/open.png"),tr("&Open..."),this);
    this->openfileAct->setShortcut(QKeySequence::Open);
    this->openfileAct->setToolTip(tr("open a image file"));
    this->openfileAct->setStatusTip(tr("Open an existing file"));
    connect(this->openfileAct,SIGNAL(triggered()),this,SLOT(openfile()));

    saveAct = new QAction(QIcon(":/res/save.png"), tr("&Save"), this);
    saveAct->setShortcuts(QKeySequence::Save);
    saveAct->setStatusTip(tr("Save the Image to disk"));
    connect(saveAct, SIGNAL(triggered()), this, SLOT(save()));

    this->quitAct = new QAction(QIcon(":/res/letter_x.png"),tr("E&xit"), this);
    quitAct->setShortcut(tr("Ctrl+Q"));
    quitAct->setStatusTip(tr("Exit the application"));
    connect(quitAct, SIGNAL(triggered()),qApp , SLOT(closeAllWindows()));

    this->rgbhisteqAct = new QAction(QIcon(":/res/letter_r.png"),tr("Rgb equalization"),this);
    rgbhisteqAct->setStatusTip(tr("Rgb histogram equalization"));
    this->yuvhisteqAct = new QAction(QIcon(":/res/letter_y.png"),tr("Yuv equalization"),this);
    yuvhisteqAct->setStatusTip(tr("Yuv histogram equalization"));

    this->orgAct = new QAction(QIcon(":/res/letter_o.png"),tr("original Image"),this);
    orgAct->setStatusTip(tr("back to original Image"));

    aboutAct = new QAction(tr("&About"), this);
    aboutAct->setStatusTip(tr("Show the application's About box"));
    connect(aboutAct, SIGNAL(triggered()), this, SLOT(about()));

    aboutQtAct = new QAction(tr("About &Qt"), this);
    aboutQtAct->setStatusTip(tr("Show the Qt library's About box"));
    connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt()));

}
void MainWindow::createMenus(){
    this->fileMenu = menuBar()->addMenu(tr("&File"));
    fileMenu->addAction(this->openfileAct);
    fileMenu->addAction(this->saveAct);
    fileMenu->addSeparator();
    fileMenu->addAction(this->quitAct);
    this->editMenu = menuBar()->addMenu(tr("&Edit"));
    editMenu->addAction(this->rgbhisteqAct);
    editMenu->addAction(this->yuvhisteqAct);
    editMenu->addAction(this->orgAct);

    this->helpMenu= menuBar()->addMenu(tr("&Help"));
    helpMenu->addAction(aboutAct);
    helpMenu->addAction(aboutQtAct);
}
void MainWindow::createToolBars(){
    this->openToolBar=this->addToolBar(tr("File"));
    this->editToolBar=this->addToolBar(tr("Edit"));
    openToolBar->addAction(this->openfileAct);
    openToolBar->addAction(this->saveAct);
    editToolBar->addAction(this->rgbhisteqAct);
    editToolBar->addAction(this->yuvhisteqAct);
    editToolBar->addAction(this->orgAct);
}
void MainWindow::createStatusBar(){
    this->statusBar()->showMessage(tr("welcome to yjy Image world"));
}
void MainWindow::openfile(){
    QString fn = QFileDialog::getOpenFileName(this,tr("open an Image file"),"./img");
    if (!fn.isEmpty()){
        this->statusBar()->showMessage(fn);
    }
}
void MainWindow::save(){

}
MainWindow::~MainWindow()
{

}

void MainWindow::about(){
    QMessageBox::about(this, tr("About midexp"),
            tr("The <b>histogram equalization</b> example demonstrates RGB & YUV histogram equalization "
               "<br/>UI using Qt."
               "<br/>Author:yjyfrombnu"
               "<br/><maito>yjyfrombnu@gmail.com</mainto>"));
}

void MainWindow::readSettings(){
    QSettings settings("conf",QSettings::IniFormat);
    QPoint pos = settings.value("pos", QPoint(200, 200)).toPoint();
    QSize size = settings.value("size", QSize(800, 500)).toSize();
    move(pos);
    resize(size);
}
void MainWindow::writeSettings(){
    QSettings settings("conf",QSettings::IniFormat);
    settings.setValue("pos", pos());
    settings.setValue("size", size());
}
void MainWindow::closeEvent(QCloseEvent *event){
    mdiArea->closeAllSubWindows();
    writeSettings();
}

void MainWindow::createDockWindows(){
    QDockWidget *dock = new QDockWidget(tr("Customers"), this);
    dock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
    QLabel * lab = new QLabel("miao");
    dock->setWidget(lab);
    this->addDockWidget(Qt::RightDockWidgetArea,dock);
}

